<template>
  <div>
    <div>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>
            <i class="el-icon-lx-cascades"></i> 物资申领列表
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="container">
        <div class="container-left" style="width: 15%">
          <el-tree
              :data="treeData.data"
              :props="{ children: 'children', label: 'label' }"
              node-key="id"
              @node-click="handleNodeClick"
          />
        </div>
        <div class="container-right">
          <div class="handle-box">
            <el-form
                :inline="true"
                :model="form"
                class="demo-form-inline"
                label-width="20px"
            >
              <el-form-item label-width="20px">
                <el-select
                    v-model="query.collegeId"
                    filterable
                    placeholder="请选择学院"
                    @focus="getAllCollege()"
                >
                  <el-option
                      v-for="item in options"
                      :key="item.sid"
                      :label="item.collegeName"
                      :value="item.sid"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select
                    v-model="query.majorId"
                    filterable
                    placeholder="请选择专业"
                    @focus="getMajor()"
                >
                  <el-option
                      v-for="item in optionsMajors"
                      :key="item.sid"
                      :label="item.majorName"
                      :value="item.sid"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select
                    v-model="query1.className1"
                    filterable
                    placeholder="请选择班级"
                    @focus="getClassList()"
                    style="width: 80%"
                >
                  <el-option
                      v-for="item in classList"
                      :key="item.className"
                      :label="item.className"
                      :value="item.className"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input
                    v-model="query.studentName"
                    placeholder="姓名"
                    class="inline-input"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="handleSearch"
                >搜索
                </el-button
                >
              </el-form-item>
            </el-form>
          </div>

          <el-table
              :data="tableData"
              border
              class="table"
              ref="multipleTable"
              header-cell-class-name="table-header"
          >
            <el-table-column label="序号" type="index" :index="indexMethod"/>
            <el-table-column
                :show-overflow-tooltip="true"
                :prop="item.prop"
                :label="item.label"
                v-for="item in props"
                :key="item.prop"
            >
            </el-table-column>
            <el-table-column>
              <el-table-column
                  label="操作"
                  width="220px"
                  align="center"
                  fixed="right"
              >
                <template #default="scope">
                  <el-button
                      type="text"
                      icon="el-icon-delete"
                      class="red"
                      @click="handleGetGoods( scope.row.studentId)"
                  >申领物资
                  </el-button
                  >
                  <!--                  <el-button-->
                  <!--                      type="text"-->
                  <!--                      icon="el-icon-edit"-->
                  <!--                      class="red"-->
                  <!--                      @click="handleAllGoods( scope.$index,scope.row.majorId)"-->
                  <!--                  >更多-->
                  <!--                  </el-button-->
                  <!--                  >-->
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination
                :currentPage="query.pageNum"
                :page-sizes="[10, 20, 40]"
                :page-size="query.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageTotal"
                @size-change="handleSizeChange"
                @current-change="handlePageChange"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <div>
      <!-- 选择物品 -->
      <el-dialog
          title="物品选择"
          v-model="dialogVisible"
          width="55%"
          :before-close="handleClose"
      >
        <div>
          <el-table
              :data="tableReceiveeData"
              border
              class="table"
              ref="multipleTable"
              header-cell-class-name="table-header"
              @selection-change="handleSelectionChange"
              @row-click="clickRow"
          >
            <!--          <el-table-column label="序号" type="index" :index="indexMethod" />-->
            <el-table-column type="selection"/>
            <el-table-column
                :show-overflow-tooltip="true"
                :prop="item.prop"
                :label="item.label"
                v-for="item in propt"
                :key="item.prop"
                align="center"
            >
            </el-table-column>
            <el-table-column label="物品样例" align="center">
              <template #default="scope">
                <p v-if="scope.row.goodsUrl!=null">
                       <span v-for="(item,index) in scope.row.goodsUrl.split(',')" :key="index">
                     <el-image style="display: inline-flex;padding: 10px;width: 30%;height: 10%"
                               class="table-td-thumb"
                               :src="item"
                               :preview-src-list="[item]"
                     ></el-image>
              </span>
                </p>
              </template>
            </el-table-column>
          </el-table>

        </div>
        <el-divider/>
        <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogVisible = false">关闭</el-button>
               <el-button @click="checkReceive">选取</el-button>
            </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {reactive, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {useRouter} from "vue-router";
import request from "../../utils/request";

export default {
  name: "GoodsReceive",
  methods: {
    clickRow(row) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
  },
  setup() {
    const router = useRouter();
    const propt = [
      {label: "物品名称", prop: "goodsName"},
      {label: "物品类型", prop: "goodsType"},
      {label: "创建时间", prop: "createTime"},
    ];
    const props = [
      {label: "学号", prop: "studentId"},
      {label: "学生姓名", prop: "studentName"},
      {label: "身份证号", prop: "idCardNo"},
      {label: "学院名称", prop: "collegeName"},
      {label: "专业名", prop: "majorName"},
      {label: "班级名称", prop: "className"},
      {label: "班级序号", prop: "classCode"},
    ];

    const treeData = reactive({
      data: [],
    });
    const queryAdviserId = reactive({
      adviserId: ""
    })
    // request.get("/college/ThreeTree",
    //     {
    //       params: queryAdviserId,
    //     }).then((res) => {
    //   treeData.data = res.data;
    // });
    request.get("/college/ThreeTree").then((res) => {
      treeData.data = res.data;
    });
    const query = reactive({
      collegeId: "",
      majorId: "",
      className: "",
      studentName: "",
      allocateDate: "",
      pageNum: 1,
      pageSize: 10,
    });
    const query1 = reactive({
      className1: "",
    });

    const tableData = ref([]);
    const pageTotal = ref(0);
    // 获取表格数据
    const getData = () => {
      tableData.value = [];
      request
          .get("/student/queryStudentIdGoods", {
            params: query,
          })
          .then((res) => {
            if (res.code != 0) {
              ElMessage.warning(res.msg);
              tableData.value = [];
            } else {
              tableData.value = res.data.records;
              pageTotal.value = res.data.total;
              query1.className1 = "";
              query.collegeId = "";
              query.majorId = "";
              query.allocateDate = "";
              query.studentName = "";
            }
          });
    };
    getData();

    // 查询操作
    const handleSearch = () => {
      query.className = query1.className1;
      query.pageNum = 1;
      getData();
    };
    // 分页大小
    const handleSizeChange = (val) => {
      query.pageSize = val;
      getData();
    };
    // 分页导航
    const handlePageChange = (val) => {
      query.pageNum = val;
      getData();
    };

    //
    const handleGetGoods = (studentId) => {
      dialogVisible.value = true;
      console.log(studentId)
      tableSelection.studentId = studentId
      getReceiveData();
    };

    //学院下拉框
    const options = ref([]);
    const getAllCollege = () => {
      console.log(query.collegeId);
      request.get("/college/getAllCollege").then((res) => {
        options.value = res.data;
        getMajor();
      });
    };
    //专业下拉框

    const optionsMajors = ref([]);
    const getMajor = () => {
      request
          .get("/major/queryList", {
            params: {collegeId: query.collegeId, type: 1},
          })
          .then((res) => {
            optionsMajors.value = res.data;
          });
    };
    //班级下拉框
    const classList = ref([]);
    const getClassList = () => {
      request
          .get("/class/byMajorIdOrCollegeId", {
            params: {majorId: query.majorId},
          })
          .then((res) => {
            classList.value = res.data;
          });
    };
    // 新增操作
    const handleAdd = () => {
      router.push("/admin/addNew");
    };

    const handleEdit = (sid) => {
      router.push({path: "/admin/addNew", query: {id: sid}});
    };

    //树状触发事件
    const handleNodeClick = (data) => {
      if (!data.children) {
        query.className = data.label;
        getData();
      }
    };
    //移除学生
    const handleAlloc = () => {

    };
    const dialogVisible = ref(false);
    const handleAllGoods = (index, majorId, sid) => {
      //todo
    };

    const tableReceiveeData = ref([]);
    // 获取表格数据
    const getReceiveData = () => {
      var studentId = tableSelection.studentId
      request
          .get("/goods/queryGoods/" + studentId)
          .then((res) => {
            if (res.data.length == 0) {
              ElMessage.info("该学生以已经物品已经全部申领");
            }
            tableReceiveeData.value = res.data;
          });
    };
    //多选
    const tableSelection = reactive({
      idList: [],
      studentId: "",
    });
    //选择列表框
    const handleSelectionChange = (val) => {
      tableSelection.idList = [];
      for (var i in val) {
        tableSelection.idList.push(val[i].sid);
      }
    };
    const checkReceive = () => {
      console.log(tableSelection)
      if (tableSelection.idList.length === 0) {
        ElMessage.info("请选择物品");
        return;
      }
      ElMessageBox.confirm("确定选择物品吗？", "提示", {
        type: "warning",
      }).then(() => {
            request
                .post("/receive/insertReceives", tableSelection)
                .then((res) => {
                  if (res.code === null) {
                    ElMessage.success("成功");
                    dialogVisible.value = false;
                    getData();
                  } else {
                    tableSelection.idList = [];
                    ElMessage.error(res.msg);
                  }
                });
          }
      );
    };
    const indexMethod = (index, number) => {
      return (index + 1)
    }
    return {
      props,
      propt,
      query,
      query1,
      options,
      optionsMajors, queryAdviserId,
      treeData,
      tableData,
      pageTotal,
      classList,
      dialogVisible,
      tableReceiveeData,
      tableSelection,
      handleSelectionChange,
      getReceiveData,
      indexMethod,
      checkReceive,
      handleAllGoods,
      getClassList,
      handleSearch,
      handleSizeChange,
      handlePageChange,
      handleAdd,
      handleGetGoods,
      handleEdit,
      handleNodeClick,
      handleAlloc,
      getAllCollege,
      getMajor,
    };
  }
  ,
}
;
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: row;
}

.container-left {
  width: 20%;
  padding-right: 20px;
}

.container-right {
  width: 80%;
  display: flex;
  flex-direction: column;
}

.btn {
  display: flex;
  text-align: left;
  padding-bottom: 10px;
}

.handle-box {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>

